package com.podhradie.net.themperature.android;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.TextView;

import com.podhradie.net.themperature.android.gauge.Gauge;
import com.podhradie.net.themperature.android.logic.Client;
import com.podhradie.net.themperature.android.logic.Constants;
import com.podhradie.net.themperature.android.logic.Values;
import com.podhradie.net.themperature.android.widget.ThemperatureWidgetProvider;

public class PodhradieTeplomerActivity extends Activity implements PropertyChangeListener {
	/** Called when the activity is first created. */
	WebView wv;
	Client client;
	Gauge gauge;
	TextView mainThemperature, mainTimeStamp;
	private ProgressDialog m_ProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
		setContentView(R.layout.main);
		gauge = (Gauge) findViewById(R.id.thermometer);
		mainThemperature = (TextView) findViewById(R.id.mainThemperature);
		mainTimeStamp = (TextView) findViewById(R.id.mainTimeStamp);
		Values.get().addChangeListener(this);
		Client.getInstance().readData(this.getApplicationContext());
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.topmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.mnuUpdate:
			Client.getInstance().readData(this.getApplicationContext());
			return true;
		case R.id.mnuSettings:
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		case R.id.mnuAbout:
			startActivity(new Intent(this, AboutActivity.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getPropertyName().equals("themperature") || arg0.getPropertyName().equals("timeStamp")) {
			gauge.setHandTarget((float) Values.get().getThemperature());
			mainThemperature.setText(String.valueOf(Values.get().getThemperatureString()));
			mainTimeStamp.setText(String.valueOf(Values.get().getTimeStamp()));
		} 
		if (arg0.getPropertyName().equals("timeStamp")) {
			// mainTimeStamp.setText(String.valueOf(Values.get().getTimeStamp()));
			Log.d("debug", "timestamp change handle");
			gauge.setHandTarget((float) Values.get().getThemperature());
			mainThemperature.setText(String.valueOf(Values.get().getThemperatureString()));
			mainTimeStamp.setText(String.valueOf(Values.get().getTimeStamp()));
			
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
			ComponentName componentName = new ComponentName(getApplicationContext(), ThemperatureWidgetProvider.class);
			int[] ids = appWidgetManager.getAppWidgetIds(componentName);
			Intent update_widget = new Intent();
			update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
			update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			update_widget.putExtra(Constants.FILL_READ_PARAM, false);
			getApplicationContext().sendBroadcast(update_widget);

		}

	}

}